package com.leyou.search.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.search.pojo.Goods;
import com.leyou.search.pojo.SearchRequest;
import com.leyou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     *  根据关键字从索引库查询
     * @param request
     * @return
     */
    // 网关定义了请求路径为/search，这里只定义后面的访问地址即可
    @PostMapping("/page")
    public ResponseEntity<PageResult<Goods>> search(@RequestBody SearchRequest request) {
        PageResult<Goods> goods = this.searchService.search(request);
        if (goods == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(goods);
    }

}
